/********************************************************************
	Rhapsody	: 7.5 
	Login		: KBE
	Component	: DefaultComponent 
	Configuration 	: CodeConfig
	Model Element	: ThreadPool
//!	Generated Date	: Tue, 25, Sep 2012  
	File Path	: DefaultComponent\CodeConfig\ThreadPool.cpp
*********************************************************************/

//## auto_generated
#include "ThreadPool.h"
//## operation ThreadPool(Reactor*)
#include "Reactor.h"
//## package Default

//## class ThreadPool
ThreadPool::ThreadPool(Reactor* reactor) {
    itsReactor = NULL;
    //#[ operation ThreadPool(Reactor*)
    itsReactor = reactor;
    //#]
}

ThreadPool::ThreadPool() {
    itsReactor = NULL;
}

ThreadPool::~ThreadPool() {
    cleanUpRelations();
}

void ThreadPool::deactivateHandle(HANDLE h, Event_Type& et) {
    //#[ operation deactivateHandle(HANDLE,Event_Type)
    itsReactor->deactivateHandle(h, et);
    //#]
}

void ThreadPool::join(int timeout) {
    //#[ operation join(int)
    //Guard<Thread_Mutex> guard(mutex_); // POSA2: Scoped Locking Idiom
    //for ( ; ; )
    //{
    //	while (leader_thread_ != NO_CURRENT_LEADER)
    //		followers_condition_.wait(timeout); // = follower role
    //	leader_thread_= Thread::self(); // Assume the leader role
    //	guard.release(); // Leave the monitor temporarily
    	// After becoming the leader,
    	// the tread uses the reactor to wait for events
    	itsReactor->handleEvents();
    //	guard.acquire(); // processing finished =>reenter the monitor
    //}
    //#]
}

void ThreadPool::promoteNewLeader() {
    //#[ operation promoteNewLeader()
    //Guard<Thread_Mutex> guard(mutex_);
    //if (leader_thread_ != Thread::self())
    	//throw; // Exception: only leader can promote !
    // indicate that we are no longer the leader
    //leader_thread_= NO_CURRENT_LEADER;
    // notify a join method to promote the next follower to leader
    //followers_condition_.notify();
    //#]
}

void ThreadPool::reactivateHandle(HANDLE h, Event_Type& et) {
    //#[ operation reactivateHandle(HANDLE,Event_Type)
    itsReactor->reactivateHandle(h, et);
    //#]
}

Reactor* ThreadPool::getItsReactor() const {
    return itsReactor;
}

void ThreadPool::cleanUpRelations() {
    if(itsReactor != NULL)
        {
            itsReactor = NULL;
        }
}

/*********************************************************************
	File Path	: DefaultComponent\CodeConfig\ThreadPool.cpp
*********************************************************************/
